package com.yixun.qfbao.api.service.impl;

import com.yixun.qfbao.api.service.AffixService;
import com.yixun.qfbao.api.service.FirstPaymentService;
import com.yixun.qfbao.apidto.AddFirstPaymentReqDto;
import com.yixun.qfbao.apidto.AffixDto;
import com.yixun.qfbao.apidto.GetFirstPaymentReqDto;
import com.yixun.qfbao.apidto.GetFirstPaymentResDto;
import com.yixun.qfbao.model.enums.AffixTypeEnum;
import com.yixun.qfbao.xf.mapper.CustomerPremisesMapper;
import com.yixun.qfbao.xf.model.CustomerPremises;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;
@Service
public class FirstPaymentServiceImpl implements FirstPaymentService{
	
	@Autowired
	private AffixService affixService;
	
	@Autowired
	private CustomerPremisesMapper customerPremisesMapper;
	
	

	@Override
	public void addFirstPayment(AddFirstPaymentReqDto reqDto) {
		CustomerPremises customerPremises=new CustomerPremises();
		customerPremises.setId(Long.parseLong(reqDto.getId()));
		customerPremises.setPayFinish(reqDto.getPayFinish());
		customerPremises.setFirstPayment(reqDto.getFirstPayment());
		customerPremises.setFirstPaymentRatio(reqDto.getFirstPaymentRatio());
		customerPremises.setUpdateTime(new Date());
		customerPremisesMapper.updateFirstPayment(customerPremises);
		
		//保存附件
		List<AffixDto> affixList=reqDto.getAffixList();
		if(!CollectionUtils.isEmpty(affixList)){
			for(AffixDto dto:affixList){
				dto.setReportId(Long.parseLong(reqDto.getId()));
				dto.setAffixUuid(UUID.randomUUID().toString());
				dto.setAffixType(AffixTypeEnum.FIRST_PAYMENT.toString());
			}
			affixService.addAffixBatch(affixList);
		}
		
	}



	@Override
	public GetFirstPaymentResDto getFirstPayment(GetFirstPaymentReqDto reqDto) {
		CustomerPremises customerPremises=customerPremisesMapper.selectById(Long.parseLong(reqDto.getId()));
		GetFirstPaymentResDto dto=new GetFirstPaymentResDto();
		if(null!=customerPremises){
			dto.setFirstPayment(customerPremises.getFirstPayment());
			dto.setFirstPaymentRatio(customerPremises.getFirstPaymentRatio());
			dto.setPayFinish(customerPremises.getPayFinish());
			dto.setId(customerPremises.getId());
		}
		return dto;
	}
	
	

}
